package com.mingqi.enetconnected.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.handler.IHandler;
import com.mingqi.enetconnected.base.handler.UIHandler;
import com.mingqi.enetconnected.view.Dialog.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by li on 2018/7/30.
 * WeChat 18571658038
 * author LiWei
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Context mContext;
    protected TextView mTVTitle,mTvRightMore;
    protected ImageView mIvRightMore,mIvLeftBack;
    protected final String mActivityName = this.getClass().getSimpleName();
    protected static UIHandler mHandler = new UIHandler(Looper.getMainLooper());
    private Unbinder mUnBinder;
    protected RelativeLayout mRlTitle;
    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_base);

        ActivityManager.getInstance().addActivity(this);
        LinearLayout llRoot = findViewById(R.id.ll_root);
        mRlTitle = findViewById(R.id.rl_title);
        View vgContent = getLayoutInflater().inflate(getLayoutID(), null);
        llRoot.addView(vgContent,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        initCommon();
        initView();
        initData();
        initClick();

        setHandler();
    }

    private void setHandler() {

        mHandler.setHandler(new IHandler() {
            @Override
            public void handleMessage(Message msg) {

                //有消息就提交给子类实现的方法
                handlerMsg(msg);
            }
        });
    }

    /**
     * 让子类处理消息
     * @param msg
     */
    protected void handlerMsg(Message msg){

    }


    private void initCommon(){

        mUnBinder = ButterKnife.bind(this);

        //初始化环境
        mContext = this;
        //初始化控件
        mTVTitle =  findViewById(R.id.tv_title);
        mTvRightMore = findViewById(R.id.tv_right_more);
        mIvRightMore = findViewById(R.id.iv_right_more);
        mIvLeftBack = findViewById(R.id.iv_left_back);

        //返回键监听
        mIvLeftBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

        //初始化进度提示框
        if(mLoadingDialog == null){

            mLoadingDialog =new LoadingDialog.Builder(mContext)
                    .setMessage("加载中...")
                    .setCancelable(true)
                    .setCancelOutSide(true)
                    .create();
        }
    }

    /**
     * 获取子类要绑定视图
     * @return
     */
    protected abstract int getLayoutID();
    /**
     * 获取子类要绑定视图
     * @return
     */
    protected abstract void initView();

    protected void initData(){

    }
    protected  void initClick(){

    }

    /**
     * 设置标题
     * @param title
     */
    protected void setTitleName(String title){
        mTVTitle.setText(title);
    }
    /**
     * 设置标题
     * @param title
     */
    protected void setTitleName(int title){
        mTVTitle.setText(title);
    }
    /**
     * 设置右侧图片
     * @param resId
     */
    protected void setRightImage(int resId,View.OnClickListener onClickListener){

        mIvRightMore.setVisibility(View.VISIBLE);
        mTvRightMore.setVisibility(View.GONE);
        mIvRightMore.setImageResource(resId);
        mIvRightMore.setOnClickListener(onClickListener);
    }
    /**
     * 设置右侧文字
     * @param text
     */
    protected void setRightTextView(String text,View.OnClickListener onClickListener){

        mTvRightMore.setVisibility(View.VISIBLE);
        mIvRightMore.setVisibility(View.GONE);
        mTvRightMore.setText(text);
        mTvRightMore.setOnClickListener(onClickListener);
    }
    /**
     * 隐藏返回键
     */
    protected void hideLeftImage() {

        findViewById(R.id.iv_left_back).setVisibility(View.GONE);
    }
    /**
     * 隐藏右侧图片
     */
    protected void hideRightImage() {

        mIvRightMore.setVisibility(View.GONE);
    }

    /**
     * Toast
     * @param object
     */
    protected void tMessage(Object object) {

        Toast.makeText(getApplicationContext(), object + "", Toast.LENGTH_SHORT).show();
    }

    protected void showProDialogHint(){

        if(mLoadingDialog.isShowing()){
            return;
        } else {

            mLoadingDialog.show();
        }
    }

    protected void hideProDialogHint(){

        if(mLoadingDialog != null){

            if(mLoadingDialog.isShowing()) {

                mLoadingDialog.dismiss();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mUnBinder != Unbinder.EMPTY) {

            mUnBinder.unbind();
        }
    }

    protected void startActivity(Class aClass){

        startActivity(aClass,null);
    }

    protected void startActivity(Class aClass,Bundle bundle){

        Intent intent = new Intent(this, aClass);
        if(bundle != null) {

            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
}
